package com.sfh.pst;



import com.sfh.base.LogUtil;
import com.sfh.pst.beauty.PstUserBeautyList;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 功能描述:动态代理
 * 实现对话提供接口实现
 *
 * @date 2017/6/7
 */
public final class ProxyHandler {


    /***
     * 获取请求层接口操作对象
     * @param service 接口类
     * @param <T>
     * @return
     * @throws RuntimeException 请求接口不存在 或请求接口未实现
     */
    public static <T> T proxy(final Class<T> service) throws RuntimeException {


        final Object objectImpl = getImplClass(service);
        return (T) Proxy.newProxyInstance(objectImpl.getClass().getClassLoader(), new Class<?>[]{service}, new InvocationHandler () {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                try {

                    return method.invoke(objectImpl, args);
                } catch (Exception e) {
                    LogUtil.e(ProxyHandler.class, "Exception:" + e.getCause());
                    return objectImpl;
                }
            }
        });
    }

    /***
     * 获取接口对于实现类
     * @param service
     * @return
     */

    private static Object getImplClass(final Class<?> service) {



        Object object = getBeautyImplClass(service);

        if (object == null) {
            object = getSubordinateImplClass(service);
        }

        if (object == null) {
            object = new Object ();
        }
        return object;
    }


    /***
     * 美人
     * @param service
     * @return
     */
    private static  Object getBeautyImplClass( Class<?> service)
    {
        if (IPstBeauty.IPstUserBeautyList.class.getName().equals(service.getName())){
            return new PstUserBeautyList ();
        }
       return null;
    }
    /***
     * 部下
     * @param service
     * @return
     */
    private static  Object getSubordinateImplClass( Class<?> service)
    {
        return null;
    }
}
